/**
 * File: SingleSubmitController.java
 * Created by: mhaimel
 * Created on: 13 Jul 2009
 * CVS:  $Id: SingleSubmitController.java,v 1.1 2009/08/05 15:16:11 mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller;

import java.io.Serializable;
import java.util.List;

import javolution.util.FastList;
import uk.ac.ebi.curtain.management.InterceptorInfo;

/**
 * @author mhaimel
 *
 */
public abstract class SingleSubmitController<I extends Serializable, O extends  Serializable> extends SimpleController<I,O> {
	private Boolean HAS_SUBMITTED = false;
	private List<I> inList = new FastList<I>();
	
	@Override
	protected O processData(I i, InterceptorInfo info) {
		getInputList().add(i);
		return null;
	}
	@Override
	protected void doShutdown() {
		syncSingleSubmit(getInputList());	
		super.doShutdown();
	}
//	@Override
//	public void shutDown(boolean force, InterceptorInfo info) {
//		if(!force){
//			syncSingleSubmit(getInputList());	
//		}
//		super.shutDown(force, info);
//	}

	private synchronized void syncSingleSubmit(List<I> inputList){
		if(!HAS_SUBMITTED){
			getLog().debug("Single submit of jobs " + getQueueName() + " ...");
			HAS_SUBMITTED = true;
			singleSubmit(inputList);
		}
	}
	
	protected abstract void singleSubmit(List<I> inputList);

	protected void setInputList(List<I> inList) {
		this.inList = inList;
	}

	protected List<I> getInputList() {
		return inList;
	}


}
